/**
 * 
 */
package de.rotkiv.common.util;


/**
 * @author Rotkiv
 *
 */
public class Toolbox {

	private static final String DELIMITER = ", ";


	/**
	 * Fills a text with n chars until it reaches a specific length
	 * @param text => the text to be filled
	 * @param c => the char to duplicate
	 * @param targetLength => the desired length of the text after filling
	 * @param fillLeft => fills left (true) or right (false)
	 * @return
	 */
	public static String fill(String text, char fillChar, int targetLength, boolean fillLeft) {
		if (text == null) {
			text = "";
		}
		int charsNeeded = targetLength - text.length();
		if (charsNeeded < 1) {
			return text;
		}
		String fillString = getRepeated(fillChar, charsNeeded);
		if (fillLeft) {
			return fillString + text;
		}

		return text + fillString;
	}

	/**
	 * @param c
	 * @param repeatCount
	 * @return a new String consting of repeatcount characters c
	 */
	public static String getRepeated(char c, int repeatCount) {
		if (repeatCount < 0) {
			return "";
		}
		char[] strg = new char[repeatCount];
		for (int i = 0; i < repeatCount; i++) {
			strg[i] = c;
		}
		return new String(strg);
	}


	public static int getStellenAnzahl(int value) {
		String str = Integer.toString(value);
		int length = str.length();
		return length;
	}


	public static String getStringFromArray(String[] arr, String delimiter) {
		int counter     = 0;
		StringBuffer sb = new StringBuffer();

		boolean useDelimiter = (delimiter != null);

		if (arr != null) {
			counter = arr.length;
		}
		for (int i = 0; i < counter; i++) {
			String temp = arr[i];
			sb.append(temp);
			if (useDelimiter && i < counter -1) {
				sb.append(delimiter);
			}
		}
		String result = sb.toString();
		return result;
	}


	public static void clearStringBuffer(StringBuffer sb) {
		int length = sb.length();
		sb.delete(0, length);
	}

	public static String toString(Object[] arr) {
		return toString(arr, DELIMITER);
	}

	public static String toString(Object[] arr, String delimiter) {
		StringBuffer sb = new StringBuffer();

		int counter = 0;
		if (arr != null) {
			counter = arr.length;
		}

		for (int i= 0; i < counter; i++) {
			if (i > 0) {
				sb.append(delimiter);
			}

			Object obj = arr[i];
			if (obj != null) {
				sb.append(obj);
			}
		}

		String result = sb.toString();
		return result;
	}


	/**
	 * Alle Vorkommen des Strings <searchWhat> innerhalb des Strings <input> durch den String <replace> ersetzen
	 */
	public static String replaceAll(String input, String searchWhat, String replace) {
		if (input == null || searchWhat == null || replace == null) {
			return input;
		}

		int index = input.indexOf(searchWhat);
		if (index < 0) {
			return input;
		}

		int length   		 = input.length();
		int lengthSearchWhat = searchWhat.length();
		int index2			 = index + lengthSearchWhat;
		String start 		 = input.substring(0, index);
		String end   		 = input.substring(index2, length);

		StringBuffer sb = new StringBuffer();
		String result = sb.append(start).append(replace).append(end).toString();

		// Rekursiver Aufruf
		result = replaceAll(result, searchWhat, replace);

		return result;
	}

}
